//
//  UIImage+Category.swift
//  FirstSwiftProduct
//
//  Created by 范泽华 on 2017/11/6.
//  Copyright © 2017年 范泽华. All rights reserved.
//

import UIKit
import Accelerate.vImage
extension UIImage {
    
    func drawRectWithCorner( radius: CGFloat, imageView: UIImageView) -> UIImage {
        //注意第三个选项的设置
        UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, false, UIScreen.main.scale)
        //在绘制之前先裁剪出一个圆形
        UIBezierPath.init(roundedRect: imageView.bounds, cornerRadius: radius).addClip()
        //图片在设置的圆形里面进行绘制
        imageView.image?.draw(in: imageView.bounds)
        //获取图片
        imageView.image = UIGraphicsGetImageFromCurrentImageContext()
        ////结束绘制
        UIGraphicsEndImageContext()
        
        return imageView.image!
    
    }
    
    /**
     图片模糊效果处理
     高斯模糊
     */
    func gaussianBlur(blurAmount:CGFloat) -> UIImage {
        //高斯模糊参数(0-1)之间，超出范围强行转成0.5
        var blurAmount = blurAmount
        
        if (blurAmount < 0.0 || blurAmount > 1.0) {
            blurAmount = 0.6
        }
        
        var boxSize = Int(blurAmount * 40)
        boxSize = boxSize - (boxSize % 2) + 1
        
        let img = self.cgImage
        
        var inBuffer = vImage_Buffer()
        var outBuffer = vImage_Buffer()
        
        let inProvider =  img!.dataProvider
        let inBitmapData =  inProvider!.data
        
        inBuffer.width = vImagePixelCount(img!.width)
        inBuffer.height = vImagePixelCount(img!.height)
        inBuffer.rowBytes = img!.bytesPerRow
        inBuffer.data = UnsafeMutableRawPointer(mutating: CFDataGetBytePtr(inBitmapData))
        
        //手动申请内存
        let pixelBuffer = malloc(img!.bytesPerRow * img!.height)
        
        outBuffer.width = vImagePixelCount(img!.width)
        outBuffer.height = vImagePixelCount(img!.height)
        outBuffer.rowBytes = img!.bytesPerRow
        outBuffer.data = pixelBuffer
        
        var error = vImageBoxConvolve_ARGB8888(&inBuffer,
                                               &outBuffer, nil, vImagePixelCount(0), vImagePixelCount(0),
                                               UInt32(boxSize), UInt32(boxSize), nil, vImage_Flags(kvImageEdgeExtend))
        if (kvImageNoError != error) {
            error = vImageBoxConvolve_ARGB8888(&inBuffer,
                                               &outBuffer, nil, vImagePixelCount(0), vImagePixelCount(0),
                                               UInt32(boxSize), UInt32(boxSize), nil, vImage_Flags(kvImageEdgeExtend))
            if (kvImageNoError != error) {
                error = vImageBoxConvolve_ARGB8888(&inBuffer,
                                                   &outBuffer, nil, vImagePixelCount(0), vImagePixelCount(0),
                                                   UInt32(boxSize), UInt32(boxSize), nil, vImage_Flags(kvImageEdgeExtend))
            }
        }
        
        let colorSpace =  CGColorSpaceCreateDeviceRGB()
        let ctx = CGContext(data: outBuffer.data,
                            width: Int(outBuffer.width),
                            height: Int(outBuffer.height),
                            bitsPerComponent: 8,
                            bytesPerRow: outBuffer.rowBytes,
                            space: colorSpace,
                            bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)
        
        let imageRef = ctx!.makeImage()
        
        //手动申请内存
        free(pixelBuffer)
        
        return UIImage(cgImage: imageRef!)
    }
    

}
